package org.example.redpacket.controller;


import org.example.redpacket.handler.R;
import org.example.redpacket.model.dto.RedPacketActivityDTO;
import org.example.redpacket.model.dto.UserReceiveRedPacketDTO;
import org.example.redpacket.model.vo.PutRedPacketActivityVO;
import org.example.redpacket.model.vo.QryActivityAmountLeftVO;
import org.example.redpacket.model.vo.ReceiveRedPacketVO;
import org.example.redpacket.service.ActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 活动相关的 controller
 *
 * @author: hero生仔
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {
    /**
     * 分区数
     */
    public static final int PARTITION = 10;
    private final AtomicInteger atomicInteger = new AtomicInteger(0);
    @Autowired
    private ActivityService activityService;

    @GetMapping("/query-amount-left/{activityId}")
    public R QueryActivityAmountLeft(@PathVariable("activityId") String activityId) {
        BigDecimal amountLeft = activityService.queryActivityAmountLeft(activityId);
        if (amountLeft.compareTo(BigDecimal.ZERO) < 0) {
            amountLeft = BigDecimal.ZERO;
        }
        return R.ok().setData(QryActivityAmountLeftVO.builder().amountLeft(amountLeft).build());
    }

    @PostMapping("/putRedPacketActivity")
    public R putRedPacketActivity(@Valid @RequestBody RedPacketActivityDTO redPacketActivityDTO) {
        // 将金额拆分成 10 等份
        BigDecimal amount = redPacketActivityDTO.getAmount();
        BigDecimal leftAmount;
        // 被分成的等份尽量是 10 份
        int divide = amount.divide(BigDecimal.TEN, RoundingMode.DOWN).intValue();
        leftAmount = amount.subtract(BigDecimal.TEN.multiply(new BigDecimal(divide)));
        List<RedPacketActivityDTO> list = new ArrayList<>(PARTITION);
        for (int i = 0; i < PARTITION; i++) {
            RedPacketActivityDTO packetActivityDTO = RedPacketActivityDTO.builder()
                    .activityId(redPacketActivityDTO.getActivityId() + "_" + i)
                    .build();
            if (i == PARTITION - 1) {
                packetActivityDTO.setAmount(leftAmount.add(new BigDecimal(divide)));
                packetActivityDTO.setLeftAmount(leftAmount.add(new BigDecimal(divide)));
            } else {
                packetActivityDTO.setAmount(new BigDecimal(divide));
                packetActivityDTO.setLeftAmount(new BigDecimal(divide));
            }
            list.add(packetActivityDTO);
        }
        // 调用服务返回结果
        Integer insertCount = activityService.batchInsertRedPacketActivity(list);
        if (insertCount != 1) {
            insertCount = 0;
        }
        return R.ok().setData(PutRedPacketActivityVO.builder().InsertNum(insertCount).build());
    }

    @PostMapping("/receiveRedPacket")
    public R receiveRedPacket(@Valid @RequestBody UserReceiveRedPacketDTO userReceiveRedPacketDTO) {
        // 可以用来测试
        userReceiveRedPacketDTO.setRedPacketId("redPacket_" + atomicInteger.getAndIncrement());
        userReceiveRedPacketDTO.setUserId("user_" + ThreadLocalRandom.current().nextInt(500));
        BigDecimal money = activityService.receiveRedPacket(userReceiveRedPacketDTO);
        return R.ok().setData(ReceiveRedPacketVO.builder().receiveRedPacket(money).build());
    }
}
